<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
    <el-col :span="12" class="mb20">
      <el-form-item label="因子" prop="factorId">
        <el-input v-model="form.factorId" style="display: none;"  placeholder="请输入因子"/>
        <el-text>{{ form.factorName }}</el-text>
        <el-button type="primary"   @click="selectRelateSubstation">选择</el-button>
        <el-button type="primary"   @click="clearRelateSubstation">清除</el-button>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="因子代码" prop="factorCode">
        <el-input readonly v-model="form.factorCode" placeholder="请输入因子代码"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="报警值" prop="alarmVal">
        <el-input v-model="form.alarmVal" placeholder="请输入报警值"/>
      </el-form-item>
      </el-col>

			</el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog width="100%"
    class="factor-dialog" title="选择子站" v-model="factorVisible"
       draggable>
    <factor ref="factorRef" @getFactorId="getFactorId" :isReleteSelect="true" ></factor>
    </el-dialog>
</template>

<script setup lang="ts" name="ThresholdDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/environment/threshold'
import { rule } from '/@/utils/validate';
const factor = defineAsyncComponent(() => import('/@/views/environment/factor/index.vue'));
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
const factorVisible = ref(false);
const factorRef = ref();
// 定义字典

// 提交表单数据
const form = reactive({
		id:'',
	  factorId: '',
    factorName:'',
	  factorCode: '',
	  alarmVal: '',
});

// 定义校验规则
const dataRules = ref({
        factorId: [{required: true, message: '因子不能为空', trigger: 'blur'}],
        factorCode: [{required: true, message: '因子代码不能为空', trigger: 'blur'}],
    alarmVal: [{required: true, message: '报警值不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
})

//打开子站选择
const selectRelateSubstation = ()=>{
  factorVisible.value=true;
  
};
const getFactorId =(row)=>{
  console.log('getFactorId row:'+row);
  form.factorName=row.name;
  form.factorCode=row.factorCode;
  form.factorId=row.id;
  factorVisible.value=false;
}

const clearRelateSubstation=()=>{
  form.factorId='';
  form.factorName='';
  form.factorCode='';
}

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''
  form.factorName = '';
  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取threshold信息
  if (id) {
    form.id = id
    getthresholdData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
    loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getthresholdData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
<style lang="less" scope>

.factor-dialog {
    // display: flex;
    // justify-content: center;
    // align-items: Center;
    overflow: hidden;
    height: 100%;
    .el-dialog {
        margin: 0 auto !important;
        position: relative;
       
        .el-dialog__header{
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            width: 100%;
            height: 60px;
            z-index: 1;
        }
        .el-dialog__body {
            width: 100%;
            overflow: hidden;
            overflow-y: auto;
            max-height: 90vh;//最大高度为视口高度的90%
            padding-top: 80px;
            padding-bottom: 100px;
            z-index: 1;            
        }
        .el-dialog__footer{
            position: absolute;
            left: 0;
            bottom: 0;
            right: 0;
            width: 100%;
            height: 80px;
            z-index: 1;
        }
    }
}


</style>  